﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GalaxyPresident
{
    public class MissileDeffend : OwnEntity,AutoAttack
    {
        private static Texture2D texture = Constant.content.Load<Texture2D>(@"Resources/MissileDefense");
        private static Texture2D texture_s = Constant.content.Load<Texture2D>(@"Resources/MissileDefense_s");
        
        private AttackingHelper _attackHelper;
        private float _rangeAttack = 400;

        public override string getObjectName()
        {
            return "Missile\nDeffend";
        }
        public Team GetTeamOwner()
        {
            return _planetOwner.TeamOwner;
        }
        public MissileDeffend(Planet planet)
            : base(planet)        
        {
            sprite_s = new Sprite(texture_s, 1, 1,
                new List<int> { 0 }, 150);
            sprite = new Sprite(texture, 2, 3,
                new List<int> {0,0,0,0, 0, 1, 2, 3, 4, 5,5,5,5,5, 4, 3, 2, 1 }, 150);

            initSprites(planet);

            _attackHelper = new AttackingHelper(new AutoChaseMissile(this));
        }

        protected override void initInfo()
        {
            meterialCost = 500;
            moneyCost = 800;
            energyCost = 200;

            _healthPoint = 2000;
            _defense = 0.3f;
        }

        public void attackTo(PlanetObjectOwner obj)
        {
            _attackHelper.attackTo(obj);
        }
        public float GetRange()
        {
            return _rangeAttack;
        }
        public override void UpdateInfo(GameTime gameTime)
        {
            base.UpdateInfo(gameTime);

            _currentTotalTime += gameTime.ElapsedGameTime.Milliseconds;
            if (_currentTotalTime >= 300)
            {
                _currentTotalTime = 0;
                Unit targetUnit = findTargetUnit();
                if (targetUnit != null)
                {
                    attackTo(targetUnit);
                    _attackHelper.attackProcessing(Position);
                }
            }
        }

        private Unit findTargetUnit()
        {
            foreach (Unit unit in PlanetOwner.Units)
            {
                if ((unit.PlanetOwner.TeamOwner != _planetOwner.TeamOwner) && (unit.Position - position).Length() <= _rangeAttack)                
                    return unit;                
            }
            return null;
        }

        public override string getInfoString()
        {
            string autoChaseStr = (_attackHelper.Ammo.IsAutoChase) ? "\nAuto chase" : "\nNo auto chase";
            return base.getInfoString()
                + "\nLevel   : " + (level + 1).ToString()
                + "\nRange   : " + _rangeAttack.ToString()
                + "\nPower   : " + _attackHelper.Ammo.Power.ToString()
                + autoChaseStr;
        }
    }
}
